package com.hmf.service;

import com.hmf.bean.Bicycle;
import com.hmf.bean.Control;
import com.hmf.bean.RideRecord;
import com.hmf.bean.User;
import com.hmf.dao.HmfListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service//告诉spring对象，我要把这个类创建对象放入spring容器
public class HmfListServiceImpl implements HmfListService {
    @Autowired//依赖注入，我从容器里面取对象
    HmfListMapper hmfListMapper;

    //根据单车编号查询是否存在该单车
    public Bicycle queryBicycleByNumber(String bicycleNumber) {
        Bicycle bicycle = hmfListMapper.queryBicycleByNumber(bicycleNumber);
        return bicycle;
    }

    //增加单车种类
    public int addBicycle(Bicycle bicycle) {
        Map<String,Object> params = new HashMap<>();
        // 查询单车是否已存在
        Bicycle aBicycle = hmfListMapper.queryBicycleByNumber(bicycle.getBicycleNumber());
        if (aBicycle != null && aBicycle.getBicycleNumber().equals(bicycle.getBicycleNumber())) {
            System.out.println("该单车类型已经存在.");
            return 1; // 单车种类已存在
        } else {
            params.put("bicycleNumber",bicycle.getBicycleNumber());
            params.put("location",bicycle.getLocation());
            params.put("status",bicycle.getStatus());
            params.put("store",bicycle.getStore());
            return hmfListMapper.addBicycle(params);
        }
    }

    //删除单车
    public int deleteBicycle(String bicycleNumber) {
        // 根据单车编号查询单车信息
        Bicycle bicycle = hmfListMapper.queryBicycleByNumber(bicycleNumber);
        if (bicycle != null && bicycle.getBicycleNumber().equals(bicycleNumber)) {
            // 执行删除操作
            int result = hmfListMapper.deleteBicycle(bicycleNumber);
            if (result == 1) {
                System.out.println("删除成功");
                return 0;
            }
        }
        // 单车不存在或单车编号输入错误导致删除失败
        System.out.println("删除失败");
        return 2;
    }

    //修改某种单车信息
    public int updateBicycle(Bicycle bicycle){
        Map<String,Object> params = new HashMap<>();
        params.put("bicycleNumber",bicycle.getBicycleNumber());
        params.put("location",bicycle.getLocation());
        params.put("status",bicycle.getStatus());
        params.put("store",bicycle.getStore());
        return hmfListMapper.updateBicycle(params);
    }

    //查询某种单车的信息
    public Bicycle searchBicycle(String bicycleNumber) {
        Bicycle bicycle = hmfListMapper.searchBicycle(bicycleNumber);
        if (bicycle!=null){
            System.out.println("查询成功");
            return bicycle;
        }else{
            System.out.println("查询失败");
            return null;
        }
    }

    public List<Bicycle> getBicyclesByPage(Map<String,Object> map) {
        List<Bicycle> bicycle1 = hmfListMapper.getBicyclesByPage(map);
        if (bicycle1 != null) {
            System.out.println("分页查询成功");
            return bicycle1;
        } else {
            System.out.println("分页查询失败");
            return null;
        }
    }
}
